﻿#region

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web.Mvc;
using Tateeda.ClinicalStudy.Base.Modules.Infrastructure.Interfaces.Services;
using Tateeda.ClinicalStudy.Base.Modules.Models.Medication;
using Tateeda.ClinicalStudy.Base.Web.Common;

#endregion

#region - Legal -

///<legal>
/// Copyright © 2011, Tateeda Media Network
/// All rights reserved. http://tateeda.com
///
/// Redistribution and use in source and binary forms, with or without
/// modification, are permitted provided that the following conditions
/// are met:
///
/// - Redistributions of source code must retain the above copyright
/// notice, this list of conditions and the following disclaimer.
///
/// - Neither the name of the Tateeda Media Network, nor the names of its
/// contributors may be used to endorse or promote products
/// derived from this software without specific prior written
/// permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
/// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
/// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
/// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
/// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
/// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES INCLUDING,
/// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
/// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
/// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
/// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
/// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
/// POSSIBILITY OF SUCH DAMAGE.
///</legal>

#endregion - Legal -

namespace Tateeda.ClinicalStudy.Base.Web.Controllers {

    [Serializable]
    public class MedicationController : Controller {
        private readonly IMedicationService _service;

        public MedicationController(IMedicationService service) {
            _service = service;
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public void AddNewPrescriptionCategory(string name) {
            var drugCategory = new DrugCategoryModel();
            drugCategory.Name = name;
            drugCategory.CreatedOn = DateTime.UtcNow;
            _service.AddNewDrugCategory(drugCategory);
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public void AddNewPrescriptionClass(string name, int categoryId) {
            var drugClass = new DrugClassModel();
            drugClass.Name = name;
            drugClass.DrugCategoryId = categoryId;
            drugClass.CreatedOn = DateTime.UtcNow;

            _service.AddNewDrugClass(drugClass);
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public void AddNewPrescriptionDrug(string name, int classId, int drugTypeId) {
            var drug = new DrugModel();
            drug.Name = name;
            drug.DrugClassId = classId;
            drug.DrugTypeId = drugTypeId;
            drug.CreatedOn = DateTime.UtcNow;
            _service.AddNewDrug(drug);
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public void DeletePrescriptionCategory(int categoryId) {
            _service.DeleteDrugCategory(categoryId);
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public void DeletePrescriptionClass(int classId) {
            _service.DeleteDrugClass(classId);
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public void DeletePrescriptionDrug(int drugId) {
            _service.DeleteDrug(drugId);
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public JsonResult GetPrescriptionDrugCategory() {
            var list = _service.GetDrugCategories();
            var result = new Collection<DrugCategoryModel>();
            foreach (var item in list) {
                var cat = new DrugCategoryModel();
                cat.DrugCategoryId = item.DrugCategoryId;
                cat.Name = item.Name;
                result.Add(cat);
            }
            return Json(result);
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public JsonResult GetPrescriptionDrugClass(int categoryId) {
            var list = _service.GetDrugClassesForCategory(categoryId);
            var result = new Collection<DrugClassModel>();
            foreach (var item in list) {
                var cls = new DrugClassModel();
                cls.DrugClassId = item.DrugClassId;
                cls.Name = item.Name;
                result.Add(cls);
            }
            return Json(result);
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public JsonResult GetPrescriptionDrugName(int classId) {
            return GetPrescriptionDrugNamePaged(classId, 0, 100);
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public JsonResult GetPrescriptionDrugNamePaged(int classId, int pageIndex, int pageSize) {
            int cnt = 0;
            var list = _service.GetDrugsByClass(classId, ref cnt, pageIndex, pageSize);

            var result = new { drugs = new Collection<DrugModel>(), total = cnt };
            foreach (var item in list) {
                var drug = new DrugModel();
                drug.DrugId = item.DrugId;
                drug.Name = item.Name;
                drug.DrugClassId = item.DrugClassId;
                result.drugs.Add(drug);
            }
            return Json(result);
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public ActionResult Index() {
            return View();
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public void UpdateDrugName(int drugId, string name) {
            _service.EditDrugName(drugId, name);
        }
    }
}